﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Managers {
    import flash.events.*;
    import Engine.Events.*;
    import flash.display.*;
    import flash.utils.*;
    import com.adobe.images.*;
    import mx.utils.*;

    public class FreezeManager extends EventDispatcher {

        private static var m_instance:FreezeManager;

        protected const STATE_THAWED:int = 0;
        protected const STATE_RENDER_FROZEN:int = 1;
        protected const STATE_ALL_FROZEN:int = 2;
        protected const CLICK_TO_THAW_TIME:int = 3000;

        public var freezingUnlocked:Boolean = false;
        private var m_frozenBitmap:Bitmap;
        private var m_frozenSprite:Sprite;
        private var m_frozenDispObj:Array = null;
        protected var m_state:int = 0;
        protected var m_frozenTime:Number = 0;
        protected var m_postThawCallbackArray:Array;

        public function FreezeManager(){
            this.m_postThawCallbackArray = [];
            super();
        }
        public static function getInstance():FreezeManager{
            if (m_instance == null){
                m_instance = new (FreezeManager)();
            };
            return (m_instance);
        }

        public function freezeAll():void{
            var _local1:Stage;
            var _local2:BitmapData;
            var _local3:Shape;
            if (((this.freezingUnlocked) && ((this.isAllFrozen() == false)))){
                this.thawRender();
                if (GlobalEngine.stage.displayState == StageDisplayState.FULL_SCREEN){
                    GlobalEngine.stage.displayState = StageDisplayState.NORMAL;
                };
                _local1 = GlobalEngine.stage;
                _local2 = new BitmapData(_local1.stageWidth, _local1.stageHeight, false, 0);
                _local2.draw(_local1);
                _local3 = new Shape();
                _local3.graphics.beginFill(0, 0.3);
                _local3.graphics.drawRect(0, 0, _local1.stageWidth, _local1.stageHeight);
                _local3.graphics.endFill();
                _local2.draw(_local3);
                this.createFrozenSprite(_local2, this.onFrozenAllClick);
                this.freezeObjects();
                _local1.addChild(this.m_frozenSprite);
                this.m_frozenBitmap.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
                this.m_frozenTime = GlobalEngine.currentTime;
                this.m_state = this.STATE_ALL_FROZEN;
            };
        }
        public function freezeRender():void{
            var _local1:Stage;
            var _local2:Sprite;
            var _local3:DisplayObjectContainer;
            var _local4:BitmapData;
            var _local5:Shape;
            var _local6:int;
            if (((this.freezingUnlocked) && ((this.isAnythingFrozen() == false)))){
                _local1 = GlobalEngine.stage;
                _local2 = GlobalEngine.viewport;
                _local3 = _local2.parent;
                _local4 = new BitmapData(_local1.stageWidth, _local1.stageHeight, false, 0);
                _local4.draw(_local2);
                _local5 = new Shape();
                _local5.graphics.beginFill(0, 0.3);
                _local5.graphics.drawRect(0, 0, _local1.stageWidth, _local1.stageHeight);
                _local5.graphics.endFill();
                _local4.draw(_local5);
                this.createFrozenSprite(_local4, this.onFrozenRenderClick);
                _local6 = _local3.getChildIndex(_local2);
                _local3.removeChild(_local2);
                _local3.addChildAt(this.m_frozenSprite, _local6);
                this.m_state = this.STATE_RENDER_FROZEN;
                getInstance().dispatchEvent(new FreezeEvent(FreezeEvent.FROZEN));
            };
        }
        protected function freezeObjects():void{
            var _local1:Stage = GlobalEngine.stage;
            if (this.m_frozenDispObj == null){
                this.m_frozenDispObj = new Array();
            };
            var _local2:int;
            _local2 = 0;
            while (_local2 < _local1.numChildren) {
                this.m_frozenDispObj.push(_local1.getChildAt(_local2));
                _local2++;
            };
            _local2 = (_local1.numChildren - 1);
            while (_local2 >= 0) {
                _local1.removeChildAt(_local2);
                _local2--;
            };
        }
        public function pushPostThawCallback(_arg1:Function):void{
            this.m_postThawCallbackArray.push(_arg1);
        }
        public function thaw(_arg1:Boolean=false):void{
            var _local2:Function;
            var _local3:Stage;
            var _local4:DisplayObject;
            var _local5:DisplayObject;
            if (this.isAnythingFrozen()){
                if (((((this.isRenderFrozen()) || (PopupQueueManager.getInstance().hasActiveDialog()))) || (_arg1))){
                    this.thawRender();
                };
                if (this.isAllFrozen()){
                    _local3 = GlobalEngine.stage;
                    _local3.removeChild(this.m_frozenSprite);
                    this.m_frozenBitmap.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
                    if (this.m_frozenBitmap.bitmapData){
                        this.m_frozenBitmap.bitmapData.dispose();
                    };
                    this.m_frozenSprite.removeEventListener(MouseEvent.CLICK, this.onFrozenAllClick);
                    this.m_frozenSprite = null;
                    this.m_frozenBitmap = null;
                    this.freezeObjects();
                    for each (_local4 in this.m_frozenDispObj) {
                        if (_local4){
                            GlobalEngine.stage.addChildAt(_local4, GlobalEngine.stage.numChildren);
                        };
                    };
                    _local5 = GlobalEngine.stage.getChildByName("root1");
                    if (((_local5) && ((_local5 is BaseGame)))){
                        _local3.setChildIndex(_local5, 0);
                    };
                    this.m_frozenDispObj.splice(0, this.m_frozenDispObj.length);
                    this.m_state = this.STATE_THAWED;
                    InputManager.enableInput();
                    if (PopupQueueManager.getInstance().hasActiveDialog()){
                        this.freezeRender();
                    } else {
                        PopupQueueManager.getInstance().pumpPopupQueue();
                    };
                    getInstance().dispatchEvent(new FreezeEvent(FreezeEvent.THAWED));
                };
                while (this.m_postThawCallbackArray.length > 0) {
                    _local2 = this.m_postThawCallbackArray.shift();
                    _local2();
                };
            };
        }
        protected function thawRender():void{
            var _local1:DisplayObjectContainer;
            var _local2:int;
            if (this.isRenderFrozen()){
                _local1 = this.m_frozenSprite.parent;
                _local2 = _local1.getChildIndex(this.m_frozenSprite);
                _local1.removeChild(this.m_frozenSprite);
                _local1.addChildAt(GlobalEngine.viewport, _local2);
                this.m_frozenSprite.removeEventListener(MouseEvent.CLICK, this.onFrozenRenderClick);
                if (this.m_frozenBitmap.bitmapData){
                    this.m_frozenBitmap.bitmapData.dispose();
                };
                this.m_frozenBitmap = null;
                this.m_frozenSprite = null;
                this.m_state = this.STATE_THAWED;
            };
        }
        public function isRenderFrozen():Boolean{
            return ((this.STATE_RENDER_FROZEN == this.m_state));
        }
        public function isAllFrozen():Boolean{
            return ((this.STATE_ALL_FROZEN == this.m_state));
        }
        public function isAnythingFrozen():Boolean{
            return (!((this.STATE_THAWED == this.m_state)));
        }
        protected function onFrozenRenderClick(_arg1:Event):void{
            if (((this.isRenderFrozen()) && ((PopupQueueManager.getInstance().hasActiveDialog() == false)))){
                this.thaw();
            };
        }
        protected function onFrozenAllClick(_arg1:Event):void{
            if (((this.isAllFrozen()) && (((this.CLICK_TO_THAW_TIME + this.m_frozenTime) < GlobalEngine.currentTime)))){
                this.thaw();
            };
        }
        public function removeFrozenPopup(_arg1:DisplayObject):void{
            var _local2:int;
            if (this.m_frozenDispObj){
                _local2 = 0;
                while (_local2 < this.m_frozenDispObj.length) {
                    if ((this.m_frozenDispObj[_local2] as DisplayObject) == _arg1){
                        this.m_frozenDispObj.splice(_local2, 1);
                        break;
                    };
                    _local2++;
                };
            };
        }
        protected function onEnterFrame(_arg1:Event):void{
            if (this.isAllFrozen()){
                GlobalEngine.zaspManager.preUpdate();
                getInstance().dispatchEvent(new FreezeEvent(FreezeEvent.UPDATE));
                GlobalEngine.zaspManager.postUpdate();
            };
        }
        public function exportScreenshot():String{
            var _local6:Base64Encoder;
            var _local7:String;
            var _local1:String;
            var _local2:Stage = GlobalEngine.stage;
            var _local3:BitmapData = new BitmapData(_local2.stageWidth, _local2.stageHeight, false, 0);
            _local3.draw(_local2);
            var _local4:Shape = new Shape();
            _local4.graphics.beginFill(0, 0.3);
            _local4.graphics.drawRect(0, 0, _local2.stageWidth, _local2.stageHeight);
            _local4.graphics.endFill();
            _local3.draw(_local4);
            var _local5:ByteArray = PNGEncoder.encode(_local3);
            if (_local5){
                _local6 = new Base64Encoder();
                _local6.encodeBytes(_local5);
                _local7 = _local6.toString();
                if (_local7){
                    _local1 = _local7;
                };
            };
            return (_local1);
        }
        protected function createFrozenSprite(_arg1:BitmapData, _arg2:Function=null):void{
            this.m_frozenBitmap = new Bitmap(_arg1);
            this.m_frozenSprite = new Sprite();
            this.m_frozenSprite.addChild(this.m_frozenBitmap);
            if (_arg2 != null){
                this.m_frozenSprite.mouseEnabled = true;
                this.m_frozenSprite.mouseChildren = true;
                this.m_frozenSprite.addEventListener(MouseEvent.CLICK, _arg2);
            };
        }

    }
}//package Engine.Managers 
